﻿using System.Net.Http.Json;
using Newtonsoft.Json;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace Production.RazorLib.Services;

public class ProductionOrderService : IProductionOrderService
{
    private readonly IHttpClientFactory _httpClientFactory;

    public ProductionOrderService(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }
    
    public async Task<PaginatedResult<ProductionOrderInfoResponseDto>?> QueryProductionOrdersByPage(
        ProductionOrderSearchRequestDto model)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("productionApi");
            var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.ProductionOrder.QueryProductionOrders));
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<PaginatedResult<ProductionOrderInfoResponseDto>>(resultJson);
                }
            }

            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public async Task<Result<ProductionOrderInfoResponseDto>?> CreateProductionOrder(ProductionOrderEditRequestDto editRequestDto)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("productionApi");
            var response = await httpClient.PostAsJsonAsync($"/{ApiRoutes.ProductionOrder.CreateProductionOrder}", editRequestDto);
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<Result<ProductionOrderInfoResponseDto>>(resultJson);
                }
            }

            return new Result<ProductionOrderInfoResponseDto>();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public async Task<Result<ProductionOrderInfoResponseDto>?> GetProductionOrder(int key)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("productionApi");
            var response = await httpClient.GetAsync($"/{ApiRoutes.ProductionOrder.GetProductionOrder.Replace("{id}", key.ToString())}");
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<Result<ProductionOrderInfoResponseDto>>(resultJson);
                }
            }

            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public async Task<Result<ProductionOrderInfoResponseDto>?> UpdateProductionOrder(int id, ProductionOrderEditRequestDto editRequestDto)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("productionApi");
            var response = await httpClient.PutAsJsonAsync($"/{ApiRoutes.ProductionOrder.UpdateProductionOrder.Replace("{id}", id.ToString())}", editRequestDto);
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<Result<ProductionOrderInfoResponseDto>>(resultJson);
                }
            }

            return new Result<ProductionOrderInfoResponseDto>();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}